package com.liqi.statistics.service.impl;


import java.util.List;

import com.liqi.common.annotation.DataScope;
import com.liqi.common.utils.DateUtils;
import com.liqi.deployment.domain.DvDeployment;
import com.liqi.deployment.mapper.DvDeploymentMapper;
import com.liqi.statistics.domain.DvServiceStatistics;
import com.liqi.statistics.mapper.DvServiceStatisticsMapper;
import com.liqi.statistics.service.IDvServiceStatisticsService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 事项统计点击数Service业务层处理
 * 
 * @author zh
 * @date 2022-10-28
 */
@Service
public class DvServiceStatisticsServiceImpl implements IDvServiceStatisticsService 
{
    @Autowired
    private DvServiceStatisticsMapper dvServiceStatisticsMapper;
    @Autowired
    private DvDeploymentMapper dvDeploymentMapper;
    /**
     * 查询事项统计点击数
     * 
     * @param id 事项统计点击数主键
     * @return 事项统计点击数
     */
    @Override
    public DvServiceStatistics selectDvServiceStatisticsById(Long id)
    {
        return dvServiceStatisticsMapper.selectDvServiceStatisticsById(id);
    }

    /**
     * 查询事项统计点击数列表
     * 
     * @param dvServiceStatistics 事项统计点击数
     * @return 事项统计点击数
     */
    @Override
    @DataScope(deptAlias = "d" , userAlias = "u",regionAlias = "r",groupAlias = "g")
    public List<DvServiceStatistics> selectDvServiceStatisticsList(DvServiceStatistics dvServiceStatistics)
    {
        return dvServiceStatisticsMapper.selectDvServiceStatisticsList(dvServiceStatistics);
    }


    /**
     * 新增事项统计点击数
     * 
     * @param dvServiceStatistics 事项统计点击数
     * @return 结果
     */
    @Override
    public int insertDvServiceStatistics(DvServiceStatistics dvServiceStatistics)
    {
        dvServiceStatistics.setCreateTime(DateUtils.getNowDate());
        return dvServiceStatisticsMapper.insertDvServiceStatistics(dvServiceStatistics);
    }
    /**
     *
     * @param itemId
     * @param deploymentCode
     * @return
     */
    @Override
    public int dvInsert(long itemId, String deploymentCode,String serviceName) {
        DvServiceStatistics dvServiceStatistics = new DvServiceStatistics();

        //通过设备自编码查找设备表得到插入日志所需设备信息，进行填充
        DvDeployment temp = new DvDeployment();
        temp.setDeploymentCode(deploymentCode);
        List<DvDeployment> list = dvDeploymentMapper.selectDvDeploymentList(temp);
        if(list.size() == 1) {
            DvDeployment dvDeployment = list.get(0);
            dvServiceStatistics.setCreateTime(DateUtils.getNowDate());
            dvServiceStatistics.setAncestors(dvDeployment.getAncestors());
            dvServiceStatistics.setAncestorsName(dvDeployment.getAncestorsName());
            dvServiceStatistics.setRegionId(Long.parseLong(dvDeployment.getRegionId()));
            dvServiceStatistics.setClickNum(1l);

            //通过itemid获取事项名称
//        String serviceName = sysItemsMapper.getItemName(itemId);
//        itemStatistics.setItemId(itemId);
            dvServiceStatistics.setServiceName(serviceName);
            return dvServiceStatisticsMapper.insertDvServiceStatistics(dvServiceStatistics);
        }else {
            return 0;
        }
    }
    

    /**
     * 修改事项统计点击数
     * 
     * @param dvServiceStatistics 事项统计点击数
     * @return 结果
     */
    @Override
    public int updateDvServiceStatistics(DvServiceStatistics dvServiceStatistics)
    {
        dvServiceStatistics.setUpdateTime(DateUtils.getNowDate());
        return dvServiceStatisticsMapper.updateDvServiceStatistics(dvServiceStatistics);
    }

    /**
     * 批量删除事项统计点击数
     * 
     * @param ids 需要删除的事项统计点击数主键
     * @return 结果
     */
    @Override
    public int deleteDvServiceStatisticsByIds(Long[] ids)
    {
        return dvServiceStatisticsMapper.deleteDvServiceStatisticsByIds(ids);
    }

    /**
     * 删除事项统计点击数信息
     * 
     * @param id 事项统计点击数主键
     * @return 结果
     */
    @Override
    public int deleteDvServiceStatisticsById(Long id)
    {
        return dvServiceStatisticsMapper.deleteDvServiceStatisticsById(id);
    }
    
        /**
     * 查询事项统计点击数列表
     * 
     * @param dvServiceStatistics 事项统计点击数
     * @return 事项统计点击数
     */
    @Override
    public List<DvServiceStatistics> selectDvServiceFrequencyStatisticsList(DvServiceStatistics dvServiceStatistics)
    {
        return dvServiceStatisticsMapper.selectDvServiceFrequencyStatisticsList(dvServiceStatistics);
    }
}
